package com.example.function;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.database.DatabaseHelper;
import com.example.pojo.Teacher;
import com.example.student.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddStaff extends AppCompatActivity {
    // 视图组件
    private EditText etTeacherId, etTeacherName, etTeacherAge, etOffice, etTeacherPhone;
    private Button btnSubmit;
    private Spinner spinnerCollege, spinnerJob;

    private DatabaseHelper databaseHelper;
    private Map<String, List<String>> collegeJobMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_staff); // 绑定布局文件
        databaseHelper = new DatabaseHelper(this);
        // 初始化视图
        initViews();
        initCollegeJobData();
        // 设置提交按钮点击事件
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addStaffToDatabase();
            }
        });
    }
    /**
     * 初始化视图
     */
    private void initViews() {
        etTeacherId = findViewById(R.id.etTeacherId);
        etTeacherName = findViewById(R.id.etTeacherName);
        etTeacherAge = findViewById(R.id.etTeacherAge);
        etOffice = findViewById(R.id.etOffice);
        etTeacherPhone = findViewById(R.id.etTeacherPhone);
        btnSubmit = findViewById(R.id.btnSubmit);
        spinnerCollege = findViewById(R.id.spinnerCollege);
        spinnerJob = findViewById(R.id.spinnerJob);
    }
    /**
     * 初始化学院和职位数据
     */
    private void initCollegeJobData() {
        // 初始化学院 Spinner
        ArrayAdapter<CharSequence> collegeAdapter = ArrayAdapter.createFromResource(this,
                R.array.college_array, android.R.layout.simple_spinner_item);
        collegeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCollege.setAdapter(collegeAdapter);

        // 初始化职位 Spinner
        ArrayAdapter<CharSequence> jobAdapter = ArrayAdapter.createFromResource(this,
                R.array.job_array, android.R.layout.simple_spinner_item);
        jobAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerJob.setAdapter(jobAdapter);
    }
    /**
     * 添加教师信息到数据库
     */
    private void addStaffToDatabase() {
        // 获取输入内容
        String teacherId = etTeacherId.getText().toString().trim();
        String teacherName = etTeacherName.getText().toString().trim();
        String ageStr = etTeacherAge.getText().toString().trim();
        String college = spinnerCollege.getSelectedItem().toString();
        String job = spinnerJob.getSelectedItem().toString();
        String office = etOffice.getText().toString().trim();
        String phone = etTeacherPhone.getText().toString().trim();

        // 校验输入
        if (teacherId.isEmpty() || teacherName.isEmpty() || ageStr.isEmpty() || college.isEmpty() || job.isEmpty() || office.isEmpty() || phone.isEmpty()) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }
        // 校验工号是否为数字
        if (teacherId.length()!=6 || !teacherId.matches("\\d+")) {
            Toast.makeText(this, "工号必须为6位数字", Toast.LENGTH_SHORT).show();
            return;
        }
        // 校验年龄是否为 2 位数字
        if (ageStr.length() != 2 || !ageStr.matches("\\d+")) {
            Toast.makeText(this, "请输入正确的年龄！", Toast.LENGTH_SHORT).show();
            return;
        }
        // 校验手机号是否为 2 位数字
        if (phone.length() > 11 || !ageStr.matches("\\d+")) {
            Toast.makeText(this, "请输入正确的手机号！", Toast.LENGTH_SHORT).show();
            return;
        }

        int age = Integer.parseInt(ageStr); // 将年龄转换为整数

        // 创建 Teacher 对象
        Teacher teacher = new Teacher(teacherId, teacherName, college, office, phone, job, age);
        teacher.setPassword("123456");
        // 插入数据库
        boolean isSuccess = databaseHelper.insertTeacher(teacher);
        if (isSuccess) {
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
            finish(); // 关闭当前页面
        } else {
            Toast.makeText(this, "添加失败，工号有误！", Toast.LENGTH_SHORT).show();
        }
    }

}